﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class RWGLExport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2010; i <= DateTime.Now.Year; i++)
                {
                    yearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                yearDropDownList.SelectedIndex = yearDropDownList.Items.Count - 1;

                for (int i = 2010; i <= DateTime.Now.Year; i++)
                {
                    tyearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                tyearDropDownList.SelectedIndex = tyearDropDownList.Items.Count - 1;

                for (int i = 1; i <= 12; i++)
                {
                    monthDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                monthDropDownList.SelectedIndex = DateTime.Now.Month == 1 ? 0 : DateTime.Now.Month - 2;

                for (int i = 1; i <= 12; i++)
                {
                    tmonthDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                tmonthDropDownList.SelectedIndex = DateTime.Now.Month - 1;

                ServiceStationService service = new ServiceStationService();
                List<ServiceStation> stations = new List<ServiceStation>();
                stations.Add(new ServiceStation { ID = 0, Name = "所有平台" });
                stations.AddRange(service.GetALLServiceStations().ToList());
                servicestationDropDownList.DataSource = stations;
                servicestationDropDownList.DataTextField = "Name";
                servicestationDropDownList.DataValueField = "ID";
                servicestationDropDownList.DataBind();
            }
        }

        protected void OKButton_Click(object sender, EventArgs e)
        {
            ExportService service = new ExportService();
            DateTime startTime = new DateTime(Convert.ToInt32(yearDropDownList.SelectedValue), Convert.ToInt32(monthDropDownList.SelectedValue), 1);
            startTime = startTime.AddDays(-1);
            DateTime endTime = new DateTime(Convert.ToInt32(tyearDropDownList.SelectedValue), Convert.ToInt32(tmonthDropDownList.SelectedValue), 1);
            endTime = endTime.AddMonths(1);
            GridView gv = new GridView();
            gv.DataSource = service.ExportTasks(startTime.ToString(), endTime.ToString(), Convert.ToInt32(servicestationDropDownList.SelectedValue));
            gv.DataBind();

            ExportExcel.ExportExcelFromGridView(gv, "任务信息");
        }
    }
}
